<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">

      <el-row>
        <el-col :span="14">
          <el-form-item label="考核内容:" prop="nrName">
            <el-cascader v-model="dataForm.nrId" :options="khTree" ref="cascader" @change="handleChange($event)" :props="{ checkStrictly: true, value:'id', label:'nrName' }"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="条款" prop="tk">
            <el-input-number v-model="dataForm.tk" :min="1" :max="999"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="考核标准:" prop="khbz">
        <el-input type="textarea" v-model="dataForm.khbz" placeholder="考核标准"></el-input>
      </el-form-item>

      <el-form-item label="个人处罚(元):">

        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf100" >100</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf200" >200</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf500" >500</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf1000" >1000</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf2000" >2000</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf3000" >3000</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grcf5000" >5000</el-checkbox>
      </el-form-item>

      <el-form-item label="集体处罚(分):">
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtcfd5" >0.5</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtcfz1" >1</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtcfz2">2</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtcfz3">3</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtcfz4">4</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtcfz5">5</el-checkbox>
      </el-form-item>

      <el-form-item label="个人奖励(元):">
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl100">100</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl200">200</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl500">500</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl1000">1000</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl2000">2000</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl3000">3000</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.grjl5000">5000</el-checkbox>
      </el-form-item>

      <el-form-item label="集体奖励(分):">
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtjld5">0.5</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtjlz1">1</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtjlz2">2</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtjlz3">3</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtjlz4">4</el-checkbox>
        <el-checkbox :true-label="1" :false-label="0" v-model="dataForm.jtjlz5">5</el-checkbox>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/admin/syskaohestandardinfo'
import {fetchListTree} from "@/api/admin/syskaohestandardnr";

export default {
  data () {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        id: 0,
        nrId: '',
        nrName: '',
        nrIds: '',
        nrNames: '',
        tk: '',
        khbz: '',
        grcf100: 0,
        grcf200: 0,
        grcf500: 0,
        grcf1000: 0,
        grcf2000: 0,
        grcf3000: 0,
        grcf5000: 0,
        jtcfd5: 0,
        jtcfz1: 0,
        jtcfz2: 0,
        jtcfz3: 0,
        jtcfz4: 0,
        jtcfz5: 0,
        grjl100: 0,
        grjl200: 0,
        grjl500: 0,
        grjl1000: 0,
        grjl2000: 0,
        grjl3000: 0,
        grjl5000: 0,
        jtjld5: 0,
        jtjlz1: 0,
        jtjlz2: 0,
        jtjlz3: 0,
        jtjlz4: 0,
        jtjlz5: 0,
      },
      grcf:[],
      dataRule: {
        nrName:[
          { required: true, message: '请选择考核内容', trigger: 'change' },

        ],
        khbz:[
          { required: true, message: '请填写考核标准', trigger: 'blur' },
          {max:1000,message: '长度过长',trigger: 'blur'},
        ]

      },
      nrData:"",
      nrDataList:[
        {label:'label1',value:'value1',path:'path1'},
        {label:'label2',value:'value2',path:'path2'},
        {label:'label3',value:'value3',path:'path3'},
      ],
      khTree:[],
    }
  },

  methods: {
    init (id) {
      this.dataForm = {
        id:id || 0,
        nrId: '',
        nrName: '',
        nrIds: '',
        nrNames: '',
        tk: '',
        khbz: '',
        grcf100: 0,
        grcf200: 0,
        grcf500: 0,
        grcf1000: 0,
        grcf2000: 0,
        grcf3000: 0,
        grcf5000: 0,
        jtcfd5: 0,
        jtcfz1: 0,
        jtcfz2: 0,
        jtcfz3: 0,
        jtcfz4: 0,
        jtcfz5: 0,
        grjl100: 0,
        grjl200: 0,
        grjl500: 0,
        grjl1000: 0,
        grjl2000: 0,
        grjl3000: 0,
        grjl5000: 0,
        jtjld5: 0,
        jtjlz1: 0,
        jtjlz2: 0,
        jtjlz3: 0,
        jtjlz4: 0,
        jtjlz5: 0,
      }
      this.dataForm.id = id || 0;
      this.canSubmit = true;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()

        fetchListTree().then((res)=>{
          this.khTree = res.data.data;
        });
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {

      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.$notify.closeAll()
              this.$notify.warning("错误")
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.$notify.closeAll()
              this.$notify.warning("错误")
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //级联,下拉
    handleChange(event){
      let data = this.$refs['cascader'].getCheckedNodes();
      this.dataForm.nrId=data[0].data.id;
      this.dataForm.nrName=data[0].data.nrName;
      this.dataForm.nrIds=data[0].data.path;
    },

  }
}
</script>
